<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Returns a new object, with added amount of days, months, years, hours, minutes and seconds</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="class.datetimeimmutable.html">« DateTimeImmutable</a></li>
      <li style="float: right;"><a href="datetimeimmutable.construct.html">DateTimeImmutable::__construct »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.datetimeimmutable.html">DateTimeImmutable</a></li>
    <li>Returns a new object, with added amount of days, months, years, hours, minutes and seconds</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="datetimeimmutable.add" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">DateTimeImmutable::add</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.5.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">DateTimeImmutable::add</span> &mdash; <span class="dc-title">
   Returns a new object, with added amount of days, months, years, hours, minutes and seconds
  </span></p>

 </div>

 <div class="refsect1 description" id="refsect1-datetimeimmutable.add-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>DateTimeImmutable::add</strong></span>(<span class="methodparam"><span class="type"><a href="class.dateinterval.html" class="type DateInterval">DateInterval</a></span> <code class="parameter">$interval</code></span>): <span class="type"><a href="class.datetimeimmutable.html" class="type DateTimeImmutable">DateTimeImmutable</a></span></div>

  <p class="para rdfs-comment">
   Creates a new <span class="classname"><a href="class.datetimeimmutable.html" class="classname">DateTimeImmutable</a></span> object, and adds the
   specified <span class="classname"><a href="class.dateinterval.html" class="classname">DateInterval</a></span> object to this, to represent
   the new value.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-datetimeimmutable.add-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt>

     <code class="parameter">interval</code>
    </dt>

    <dd>

     <p class="para">
      A <span class="classname"><a href="class.dateinterval.html" class="classname">DateInterval</a></span> object
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-datetimeimmutable.add-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回带有修改数据的新 <span class="classname"><a href="class.datetimeimmutable.html" class="classname">DateTimeImmutable</a></span> 对象。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-datetimeimmutable.add-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-2001">
   <p><strong>示例 #1 <span class="function"><strong>DateTimeImmutable::add()</strong></span> example</strong></p>
   <div class="example-contents"><p>面向对象风格</p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$date&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTimeImmutable</span><span style="color: #007700">(</span><span style="color: #DD0000">'2000-01-01'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$newDate&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$date</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">add</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">(</span><span style="color: #DD0000">'P10D'</span><span style="color: #007700">));<br />echo&nbsp;</span><span style="color: #0000BB">$newDate</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'Y-m-d'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>

  <div class="example" id="example-2002">
   <p><strong>示例 #2 Further <span class="function"><strong>DateTimeImmutable::add()</strong></span> examples</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$date&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTimeImmutable</span><span style="color: #007700">(</span><span style="color: #DD0000">'2000-01-01'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$newDate&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$date</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">add</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">(</span><span style="color: #DD0000">'PT10H30S'</span><span style="color: #007700">));<br />echo&nbsp;</span><span style="color: #0000BB">$newDate</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'Y-m-d&nbsp;H:i:s'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$date&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTimeImmutable</span><span style="color: #007700">(</span><span style="color: #DD0000">'2000-01-01'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$newDate&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$date</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">add</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">(</span><span style="color: #DD0000">'P7Y5M4DT4H3M2S'</span><span style="color: #007700">));<br />echo&nbsp;</span><span style="color: #0000BB">$newDate</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'Y-m-d&nbsp;H:i:s'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
2000-01-01 10:00:30
2007-06-05 04:03:02
</pre></div>
   </div>
  </div>

  <div class="example" id="example-2003">
   <p><strong>示例 #3 Beware when adding months</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$date&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTimeImmutable</span><span style="color: #007700">(</span><span style="color: #DD0000">'2000-12-31'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$interval&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">(</span><span style="color: #DD0000">'P1M'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$newDate1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$date</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">add</span><span style="color: #007700">(</span><span style="color: #0000BB">$interval</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$newDate1</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'Y-m-d'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$newDate2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$newDate1</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">add</span><span style="color: #007700">(</span><span style="color: #0000BB">$interval</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$newDate2</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'Y-m-d'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
2001-01-31
2001-03-03
</pre></div>
   </div>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-datetimeimmutable.add-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member"><span class="function"><a href="datetimeimmutable.sub.html" class="function" rel="rdfs-seeAlso">DateTimeImmutable::sub()</a> - Subtracts an amount of days, months, years, hours, minutes and seconds</span></li>
   <li class="member"><span class="function"><a href="datetime.diff.html" class="function" rel="rdfs-seeAlso">DateTimeImmutable::diff()</a> - 返回两个 DateTime 之间的差值</span></li>
   <li class="member"><span class="function"><a href="datetimeimmutable.modify.html" class="function" rel="rdfs-seeAlso">DateTimeImmutable::modify()</a> - Creates a new object with modified timestamp</span></li>
  </ul>
 </div>

</div></div></div></body></html>